﻿using MediatR;
using SheepCultivation.Common.Domain.main;
using SheepCultivation.Common.Domain.Sheep;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.AddressW
{
    public class PregnancySeachHandler: IRequestHandler<PregnancySeachCommon,Result>
    {
        public readonly IBaseRepository<Pregnancy> PregnancyRepository;
        public readonly IBaseRepository<Columns> ColumnsRepository;
        public readonly IBaseRepository<SheepPregnancy> SheepPregnancyRepository;
        //public readonly IBaseRepository<BasicInformation> BasicInformationRepository;

        public PregnancySeachHandler(IBaseRepository<Pregnancy> pregnancyRepository, IBaseRepository<Columns> columnsRepository, IBaseRepository<SheepPregnancy> sheepPregnancyRepository)
        {
            PregnancyRepository = pregnancyRepository;
            ColumnsRepository = columnsRepository;
            SheepPregnancyRepository=sheepPregnancyRepository;
            //BuildingRepository= buildingRepository;
            //BasicInformationRepository = basicInformationRepository;
        }

        public async Task<Result> Handle(PregnancySeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                var list = (from a in PregnancyRepository.GetAll()
                            join b in SheepPregnancyRepository.GetAll()
                            on a.ColumnsId equals b.HouseNumber
                            join c in ColumnsRepository.GetAll()
                            on b.HouseNumber equals c.ColumnsId
                            select new PregnancyDTO
                            {
                                BuildingId=a.BuildingId,
                                BuildingName=b.HouseName,
                                PregnancyId=b.SheepPregnancyId,
                                Auricle=b.SheepPregnancyEarNumber,
                               
                                PregnancyDay= a.PregnancyDay,
                                PregnancyName = b.SheepPregnancyDays,
                                SheepId=a.SheepId,
                                ColumnsId=c.ColumnsId,
                                ColumnsName=c.ColumnsName,
                            }).ToList();

                res.Code=(int) CommonEnumCode.success;
                res.Data = list;
                res.Msg = "首次妊检提示表";
            }
            catch (Exception ex)
            {

                throw;
            }
            return res;
        }
    }
    public class PregnancyDTO
    {
        public int PregnancyId { get; set; }
        /// <summary>
        /// 种母耳号
        /// </summary>
        public int BreedingSheepArchiveId { get; set; }
        /// <summary>
        /// 种母耳号名称
        /// </summary>
        public string Auricle { get; set; }
        /// <summary>
        /// 妊检天数
        /// </summary>
        public int PregnancyDay { get; set; }
        /// <summary>
        /// 栋舍
        /// </summary>
        public int BuildingId { get; set; }
        public string BuildingName { get; set; }
        /// <summary>
        /// 栏位
        /// </summary>
        public int ColumnsId { get; set; }
        public string ColumnsName { get; set; }
        /// <summary>
        /// 妊检提示类型
        /// </summary>
        public int PregnancyName { get; set; }
        /// <summary>
        /// 羊只档案
        /// </summary>
        public int SheepId { get; set; }
    }
}
